package Iol;
import java.io.*;
import java.util.Random;
/**
 * description:
 * author:zcx
 * lenovo
 * 时间：2022.01.19.14.52
 */
public class Io3 {
    public static void main(String[] args) {
      String f="u1.txt";
      Jl(new File(f));
      Dq(new File(f));
      //Delt(new File(f));
      //Update(new File(f));
      Copy(new File(f));
    }
    //建立文件
    public static void Jl(File f0){
        try(var w=new BufferedWriter(new FileWriter(f0))) {
            Random r=new Random();
            w.write(String.format("%07d [%tF %<tT]",r.nextInt(1,9999),System.currentTimeMillis()));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //读取文件
    public static void Dq(File f1){
        try(var r=new BufferedReader(new FileReader(f1))) {
            r.lines().forEach(System.out::println);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //删除文件
    public static void Delt(File f2){
       if(f2.exists()){
           f2.delete();
       }
    }
    //更改文件名字
    public static void Update(File f3){
        if(f3.exists()){
            var fn=f3.getName();
           // fn="2022119.txt";
            //f3.renameTo(new File(fn));
            f3.renameTo(new File("20221191.txt"));
        }
    }
    //文件复制
    public static void Copy(File f4){
        try(var is=new BufferedInputStream(new FileInputStream(f4))) {
        is.transferTo(new FileOutputStream("u2.txt"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
